import java.util.Arrays;

class Solution {
    public boolean isInterleave(String s1, String s2, String s3) {
        int n=s1.length();
        int m=s2.length();
        if(n+m!=s3.length()){
            return false;
        }
        s1="$"+s1;
        s2="$"+s2;
        s3="$"+s3;
        boolean[][] dp=new boolean[n+1][m+1];
        //初始化
        dp[0][0]=true;
        for (int i = 1; i <= m; i++) {
            if(s2.charAt(i)==s3.charAt(i)){
                dp[0][i]=true;
            }else{
                break;
            }
        }
        for (int i = 1; i <= n; i++) {
            if(s1.charAt(i)==s3.charAt(i)){
                dp[i][0]=true;
            }else{
                break;
            }
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j]=((s3.charAt(i+j)==s1.charAt(i)) && dp[i-1][j]) || ((s3.charAt(i+j)==s2.charAt(j)) && dp[i][j-1]);
            }
        }

        return dp[n][m];
    }
}